<?php

namespace hotel\common\utils;


class HttpClient extends BaseServices
{
    private static $headers = [];

    private static $cookie = null;

    public static function get($url, $param = [])
    {
        return self::curl($url, $param, 'get');
    }

    public static function post($url, $param, $extra = [])
    {
        return self::curl($url, $param, 'post');
    }

    protected static function curl($url, $param, $method = 'post')
    {
        $calculate_time1 = microtime(true);
        // 初始华
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CERTINFO, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        //curl_setopt($curl, CURLOPT_VERBOSE, true); //打印日志

        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //函数中加入下面这条语句

        if (isset(\Yii::$app->params['curl']) && isset(\Yii::$app->params['curl']['timeout'])) {
            curl_setopt($curl, CURLOPT_TIMEOUT, \Yii::$app->params['curl']['timeout']);
        } else {
            curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        }

        if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
            curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        }

        if (!empty(self::$headers)) {
            $headerArr = [];
            foreach (self::$headers as $n => $v) {
                $headerArr[] = $n . ': ' . $v;
            }
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr); //构造IP
        }

        if (self::$cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, self::$cookie);
        }

        // post处理
        if ($method == 'post') {
            curl_setopt($curl, CURLOPT_POST, true);
            if (!empty($param)) {
                if (is_array($param)) {
                    $param = http_build_query($param);
                }
                curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
            }
        } else {
            curl_setopt($curl, CURLOPT_POST, false);
        }

        // 执行输出
        $info = curl_exec($curl);
        //log
        $_errno = curl_errno($curl);
        $_error = '';
        if ($_errno) {
            $_error = curl_error($curl);
        }
        curl_close($curl);
        $calculate_time_span = microtime(true) - $calculate_time1;
        $log = \Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'curl.log';
        file_put_contents($log, date('Y-m-d H:i:s') . " [ time:{$calculate_time_span} ] url: {$url} \nmethod: {$method} \ndata: " . json_encode($param) . " \nresult: {$info} \nerrorno: {$_errno} error: {$_error} \n", FILE_APPEND);

        if ($_error) {
            return self::_err($_error);
        }

        return $info;
    }

    public static function setHeader($header)
    {
        self::$headers = $header;
    }

    public static function setCookie($cookie)
    {
        self::$cookie = $cookie;
    }

    /**
     * 以xml形式发送post请求
     *
     * @param [type] $xml
     * @param [type] $url
     * @param boolean $useCert
     * @param integer $second
     * @param $store_type 1-上海白玉兰广场 2-杭州城西银泰店
     * @return void
     */
    public static function postXmlCurl($xml, $url, $useCert = false, $second = 30,$store_id='sh001')
    {
        $weixin_info = \Yii::$app->params['weixin'];
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);

        //如果有配置代理这里就设置代理
        if ($weixin_info['CURL_PROXY_HOST'] != '0.0.0.0' && $weixin_info['CURL_PROXY_PORT'] != 0) {
            curl_setopt($ch, CURLOPT_PROXY, $weixin_info['CURL_PROXY_HOST']);
            curl_setopt($ch, CURLOPT_PROXYPORT, $weixin_info['CURL_PROXY_PORT']);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //严格校验
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, false);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if ($useCert == true) {
            //设置证书
            //使用证书：cert 与 key 分别属于两个.pem文件
            if($store_id == 'sh001'){
                curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
                curl_setopt($ch, CURLOPT_SSLCERT, '/www/wwwroot/jlbwxcs.cnkly.com/club_ii/web/sh001_apiclient_cert.pem');
                curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
                curl_setopt($ch, CURLOPT_SSLKEY, '/www/wwwroot/jlbwxcs.cnkly.com/club_ii/web/sh001_apiclient_key.pem');
            }elseif($store_id == 'hz001'){
                curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
                curl_setopt($ch, CURLOPT_SSLCERT, '/www/wwwroot/jlbwxcs.cnkly.com/club_ii/web/hz001_apiclient_cert.pem');
                curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
                curl_setopt($ch, CURLOPT_SSLKEY, '/www/wwwroot/jlbwxcs.cnkly.com/club_ii/web/hz001_apiclient_key.pem');
            }elseif($store_id == 'cz001'){
                curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
                curl_setopt($ch, CURLOPT_SSLCERT, '/www/wwwroot/jlbwxcs.cnkly.com/club_ii/web/cz001_apiclient_cert.pem');
                curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
                curl_setopt($ch, CURLOPT_SSLKEY, '/www/wwwroot/jlbwxcs.cnkly.com/club_ii/web/cz001_apiclient_key.pem');
            }

//            if($store_id == 'sh001'){
//                curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
//                curl_setopt($ch, CURLOPT_SSLCERT, '/www/wwwroot/test.dinoman.horsevision.cn/club_ii/web/sh001_apiclient_cert.pem');
//                curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
//                curl_setopt($ch, CURLOPT_SSLKEY, '/www/wwwroot/test.dinoman.horsevision.cn/club_ii/web/sh001_apiclient_key.pem');
//            }elseif($store_id == 'hz001'){
//                curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
//                curl_setopt($ch, CURLOPT_SSLCERT, '/www/wwwroot/test.dinoman.horsevision.cn/club_ii/web/hz001_apiclient_cert.pem');
//                curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
//                curl_setopt($ch, CURLOPT_SSLKEY, '/www/wwwroot/test.dinoman.horsevision.cn/club_ii/web/hz001_apiclient_key.pem');
//            }elseif($store_id == 'cz001'){
//                curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
//                curl_setopt($ch, CURLOPT_SSLCERT, '/www/wwwroot/test.dinoman.horsevision.cn/club_ii/web/cz001_apiclient_cert.pem');
//                curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
//                curl_setopt($ch, CURLOPT_SSLKEY, '/www/wwwroot/test.dinoman.horsevision.cn/club_ii/web/cz001_apiclient_key.pem');
//            }

        }
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //运行curl
        $data = curl_exec($ch);
        //返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            var_dump($error);
            curl_close($ch);

            return false;
            //throw new \WxPayException("curl出错，错误码:$error");
        }
    }
}